package hasthi.tools.analyze;

import hasthi.common.HasthiException;
import hasthi.test.ClientSetDocument;
import hasthi.test.PerfDataDocument;
import hasthi.test.ClientSetDocument.ClientSet;
import hasthi.test.PerfDataDocument.PerfData;

import java.util.concurrent.Semaphore;

import org.apache.xmlbeans.XmlObject;

public class PerfHeadNode extends PerfAgent{
    private DataSetBag dataSet;
    private Semaphore responseRendezvous = new Semaphore(0);
    private boolean isListenerOnly = false;
    
    
    public PerfHeadNode() throws HasthiException {
        super();
    }

    @Override
    public void performTriggerAction(XmlObject event, XmlObject[] matchingResults)
            throws HasthiException {
        if(event instanceof PerfDataDocument){
            PerfData perfData = ((PerfDataDocument)event).getPerfData();
            if(isListenerOnly){
                dataSet.addData((PerfDataDocument)event);
            }else{
                if(!perfData.getProducer().equals(DataSetBag.getLocalHostName())){
                    if(perfData.getTestName().equals(dataSet.getName())){
                        dataSet.addData((PerfDataDocument)event);
                        responseRendezvous.release();
                    }else{
                        System.out.println("Data set does not match "+ perfData.getTestName() + " = " +dataSet.getName());
                    }
                }else{
                    System.out.println("Ignored self message");
                }
            }
        }
    }
    
    public void test()throws Exception{
        //int[] clientCounts = new int[]{1,5,10,25,50,75,125,250,500,750};
        int[] clientCounts = new int[]{250};
        for(int clientCount:clientCounts){
            dataSet = new DataSetBag("MngO-4*"+clientCount);
            System.out.println(4 + "*" + clientCount);
            ClientSetDocument clientSetDocument = ClientSetDocument.Factory.newInstance();
            ClientSet clientSet = clientSetDocument.addNewClientSet();
            clientSet.setClientCount(clientCount);
            clientSet.setRequestCountPerClient(100000);
            clientSet.setServiceUrl("http://hagar.cs.indiana.edu:5678/dummy");
            //clientSet.setServiceUrl("http://127.0.0.1:47594/dummy");
            clientSet.setTargetHost("*");
            clientSet.setTestName(dataSet.getName());
            clientSet.setTestTimePeriod(15*60*1000);
            
           // if(verifyService(clientSet.getServiceUrl())){
                sendEvent(clientSetDocument);
                responseRendezvous.acquire(4);
                dataSet.sendSummery();
//            }else{
//                throw new HasthiException("Error at service",FaultCode.InternalServiceError);
//            }
        }
    }
    
    public void actAsListener(){
        dataSet = new DataSetBag("PassiveListener");
        dataSet.setFilterByTestName(false);
        dataSet.setWrite2file(true);
        setListenerOnly(true);
    }
    
    
    public void setListenerOnly(boolean isListenerOnly) {
        this.isListenerOnly = isListenerOnly;
    }

    public static void main(String[] args) {
        try {
            PerfHeadNode headNode = new PerfHeadNode();
            System.out.println("Started");
            //headNode.test();
            headNode.actAsListener();
            System.out.println("Completed");
        } catch (HasthiException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
